package com.zelin.user.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zelin.entity.UserEntity;
import com.zelin.user.service.UserService;
import com.zelin.utils.PhoneFormatCheckUtils;
import com.zelin.utils.R;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/8/10-15:44
 * ------------------------------
 */
@Controller
public class UserController {
    @Reference(timeout = 5000)
    private UserService userService;
    //1.查询页面
    @RequestMapping({"/","/register.html"})
    public String list(){
        return "register";
    }

    //2. 获取验证码
    @RequestMapping("/user/getCode")
    public String getCode(String phone){
        //2.1 验证手机号
        if(PhoneFormatCheckUtils.isChinaPhoneLegal(phone)){ //手机号合格
            userService.getCode(phone);
        }
        return "register";
    }
    //3. 添加用户
    @RequestMapping("/user/add")
    public String add(UserEntity userEntity,String validCode){
        //3.1 比较用户输入的验证码与redis中的验证码是否一致
        boolean b = userService.purdgeCode(validCode,userEntity.getPhone());
        //3.2 如果比较相等，就添加用户
        if(b){
            userService.add(userEntity);
        }
        return "register";
    }

    //4. 到达用户中心主页
    @GetMapping("/index")
    public String index(Model model){
        //4.1 取得用户名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //4.2 将用户名放到model中
        model.addAttribute("name",name);
        //4.3 返回视图
        return "index";
    }
}
